﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using Oracle.ManagedDataAccess.Client;
using System.Reflection;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Drawing;
using System.IO;


namespace Meiam.System.Interfaces
{
    public class AIYS
    {
        public static string strmsg = "";

        public static string BXYS(DataTable dt,double min,double max,double thickness, double xqcounts,string bxtype,string bxtype2)
        {

                // 选择average_resistance列中值在0.4~0.8范围内的记录
                List<DataRow> selectedRows = new List<DataRow>();
                foreach (DataRow row in dt.Rows)
                {
                     selectedRows.Add(row);                   
                }

                // 随机选择xqcounts行
                List<DataRow> randomRows = new List<DataRow>();
                if (selectedRows.Count >= xqcounts)
                {
                    var rand = new Random();
                    for (int i = 0; i < xqcounts; i++)
                    {
                        int index = rand.Next(selectedRows.Count);
                        randomRows.Add(selectedRows[index]);
                        selectedRows.RemoveAt(index);
                    }
                }
                else
                {
                    randomRows = selectedRows;
                }

                // 计算选定行的平均值
                double average = 0;
                double average2 = 0;
            double average_resistance = 0;
                 double average_thickness = 0;
            foreach (DataRow row in randomRows)
                {
                    average += Convert.ToDouble(row[bxtype]);
                    average2+= Convert.ToDouble(row[bxtype2]);
            }

            average_resistance = average /randomRows.Count;
            average_thickness = average2 / randomRows.Count;

            // 如果平均值在0.5~0.6范围内，则返回结果的行记录
            List<DataRow> resultRows = new List<DataRow>();
                if (Math.Round(average_resistance,2) >= min && Math.Round(average_resistance, 2) <= max)
                {
                if ( Math.Round(average_thickness,3)>= thickness)
                {
                    foreach (DataRow row in randomRows)
                    {

                        resultRows.Add(row);

                    }
                }
                   
                }
         

                 string barcode_no = "";
            if (resultRows.Count>0)
            {
                // 输出结果的行记录
                foreach (DataRow row in resultRows)
                {
                    barcode_no = barcode_no + "'" + row["barcode_no"].ToString().Trim() + "',";
                    //redt.Rows.Add(row);               
                }
            }
            barcode_no= barcode_no.TrimEnd(',');



            return barcode_no;
        }


    }
}




